"""
Case Type   : cm_ctl可靠性
Case Name   : cm_ctl stop -n 故障测试-cm_ctl stop-n故障前手动新增文件cluster_manual_start
Create At   : 2022-08-11
Owner       : opentestcase012
Description :
    1: 查看集群状态
    2: $GAUSSHOME/bin下新增文件cluster_manual_start
    3: 查看集群状态
    4: 执行cm_ctl stop -n命令
    5: 查看集群状态
    6: 删除文件文件cluster_manual_start
    7: 查看集群状态
    8：启动节点
    9：查看集群状态
Expect      :
    1: 集群状态正常
    2: 成功
    3: 集群状态异常
    4: 成功
    5: 集群状态异常
    6: 成功
    7: 集群状态正常
    8：成功
    9：集群状态正常
History     :
"""

import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_user = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.sta1_user = Node('Standby1DbUser')
        self.constant = Constant()
        self.common = Common()
        self.start_file = os.path.join(macro.GAUSSHOME_BIN_PATH,
                                       'cluster_manual_start')

    def test_cm(self):
        text = '----step1:查看集群状态; expect:集群状态正常----'
        self.log.info(text)
        time.sleep(5)
        show_status = f'source {macro.DB_ENV_PATH};' \
            f'cm_ctl query -Civ'
        self.log.info(show_status)
        status_msg = self.common.get_sh_result(self.pri_user, show_status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_msg,
                      "执行失败:" + text)

        try:
            text = '----step2:$GAUSSHOME/bin下新增文件cluster_manual_start; ' \
                   'expect:成功----'
            self.log.info(text)
            touch_cmd = f'touch {self.start_file};'
            self.log.info(touch_cmd)
            touch_msg = self.common.get_sh_result(self.sta1_user, touch_cmd)
            self.assertEqual('', touch_msg, "执行失败:" + text)

            text = '----step3:查看集群状态; expect:集群状态异常，备节点停止----'
            self.log.info(text)
            time.sleep(10)
            self.log.info(show_status)
            status_msg = self.common.get_sh_result(self.pri_user, show_status)
            self.assertIn(self.constant.CLUSTER_DEGRADED_MSG, status_msg,
                          "执行失败:" + text)
            self.assertIn('6002     S Down    Unknown', status_msg,
                          "执行失败:" + text)

            text = '----step4:执行cm_ctl stop -n命令; expect:stop成功----'
            self.log.info(text)
            stop_cmd = f'source {macro.DB_ENV_PATH};' \
                f'cm_ctl stop -n 2 ;'
            self.log.info(stop_cmd)
            stop_msg = self.common.get_sh_result(self.pri_user, stop_cmd)
            self.assertIn(self.constant.cm_stop_node_success_msg, stop_msg,
                          "执行失败:" + text)

            text = '----step5:查看集群状态; expect:集群状态异常，备节点停止----'
            self.log.info(text)
            time.sleep(5)
            self.log.info(show_status)
            status_msg = self.common.get_sh_result(self.pri_user, show_status)
            self.assertIn(self.constant.CLUSTER_DEGRADED_MSG, status_msg,
                          "执行失败:" + text)
            self.assertIn('6002     S Down    Unknown', status_msg,
                          "执行失败:" + text)

        finally:
            text = '----step6:删除文件文件cluster_manual_start; expect:成功----'
            self.log.info(text)
            restart_cmd = f'rm -rf {self.start_file};' \
                     f'source {macro.DB_ENV_PATH};' \
                     f'cm_ctl start -n 2 ;'
            self.log.info(restart_cmd)
            rm_msg = self.common.get_sh_result(self.sta1_user, restart_cmd)
            self.assertIn(self.constant.cm_start_node_success_msg, rm_msg, "执行失败:" + text)

        text = '----step7:查看集群状态; expect:集群状态正常----'
        self.log.info(text)
        time.sleep(10)
        self.log.info(show_status)
        status_msg = self.common.get_sh_result(self.pri_user, show_status)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_msg,
                      "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '-----step8:启动节点; expect:成功-----'
        self.log.info(text1)
        start_cmd = f'source {macro.DB_ENV_PATH};' \
            f'cm_ctl start -n 2;'
        self.log.info(start_cmd)
        start_msg = self.common.get_sh_result(self.pri_user, start_cmd)

        text2 = '----step9:查看集群状态; expect:集群状态正常----'
        self.log.info(text2)
        time.sleep(5)
        show_status = f'source {macro.DB_ENV_PATH};' \
            f'cm_ctl query -Civ'
        self.log.info(show_status)
        status_msg = self.common.get_sh_result(self.pri_user, show_status)

        self.assertIn(self.constant.cm_start_node_success_msg, start_msg,
                      "执行失败:" + text1)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_msg,
                      "执行失败:" + text2)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
